package com.kgc.schedule;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.kgc.api.user.UserInfoApi;
import com.kgc.bean.User;

import com.kgc.config.BuyLimitConfig;
import com.kgc.util.RedisUtils;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.jms.Destination;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Set;

/**
 * @Author：周申宇 <p>
 * @Date:2021/6/21 21:16
 * <p>
 * @Decription:
 */
@Component
@Slf4j
public class SendMsg {

    @Autowired
    private RedisUtils redisUtils;

    @Reference
    private UserInfoApi userInfoApi;


    @Autowired(required = false)
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * @Auther: 周申宇
     * <p>
     * @Date: 2021/6/21
     * <p>
     * @Description: 使用定时任务向已添加的用户发消息提醒
     */
    @Scheduled(cron = "10 * * * * ?")
    public void sendMsgToUser() {
        log.info("开始遍历活动，给添加抢购用户发送手机号");
        // 获取所有的活动id
        Set set = redisTemplate.opsForSet().members("schedual");
        if(!ObjectUtils.isEmpty(set) && set.size()>0 ) {
            set.forEach(activityId -> {
                // 给添加用户的手机号发送信息
                if(!ObjectUtils.isEmpty(redisUtils.get("user-" + activityId))) {
                    ArrayList<String> userIds = (ArrayList<String>) JSON.parseArray(redisUtils.get("user-" + activityId).toString(), String.class);
                    log.info("用户id为{}", userIds.toString());
                    // 先从redis中拿出来本次抢购的用户id
                    if(userIds.size()>0){
                        userIds.forEach(userId -> {
                            User user = userInfoApi.findUserBaseOnUserId(Integer.valueOf(userId));
                            log.info("手机号为：{}", user.getPhone());
                            if(!StringUtils.isEmpty(user.getPhone())){
                                sendMessageToQueue(user.getPhone());
                            }
                        });
                    }
                    redisTemplate.opsForSet().remove("schedual", activityId);
                    log.info("当前系统 时间：{}", LocalDate.now());
                }else {
                    log.info("没有需要发送的手机号");
                }
            });
        }else {
            log.info("没有需要发送的手机号");
        }



    }


    /**
     * @Auther: 周申宇
     * <p>
     * @Date: 2021/6/11
     * <p>
     * @Description: 向指定的消息队列发送消息
     */
    public void sendMessageToQueue(String phone){
        log.info("******* 2.向队列{}，发送一条队列消息：{},begin",phone);

        // 创建一个目标消息队列（消息容器）
        Destination destination = new ActiveMQQueue("snap-user-phone");

        // 向目标队列发送消息
        jmsMessagingTemplate.convertAndSend(destination,phone);

    }
}
